#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "open.h"



int search(char **tquery, struct record *records_t, int lk, char **items_t, int lenk, int si)
{
	int i,x,f,y,z=0;
	char *results[200]; //results table
	for (i=0;i<si;i++)
	{
		x=0;
		f=0;
		while ((f == 0) && (x < lk)) /* wyszukuje kod dla wprowadzonego slowa kluczowego */
		{       
			if (strcmp((records_t+x)->NAME,tquery[i])==0)
				f = 1; /* f =1 - found code which pass to keyword */
			else
				x++;
		}
		if ((f != 0) && (i == 0)) /* searching for first keyword */
			for (y=0;y<lenk;y++)
				if (strstr(items_t[y],(records_t+x)->VAL) != NULL)
				{
					results[z] = strdup(items_t[y]);
					z++;
				}
		if ((f != 0) && (i > 0)) /* serching for next keywords*/
			for (y=0;y<z;y++)
				if (results[y] != NULL)
					if (strstr(results[y],(records_t+x)->VAL) == NULL)
						results[y] = NULL; 
		
	}
	printf("\nresults:\n");
	for (i=0;i<z;i++)
		if (results[i] != NULL)
			puts(results[i]);
	return 0;
}






void srun(struct record *records_t, int lk, char **items_t, int lenk)
{
	char *str;
	char *tquery[4];
	int x=0,y; /* x : keywords counter */
	printf("enter keywords, which you want to find in stored codes (max 4).\n At the end enter '.end.'");
	do
	{
		str = malloc(10);
		scanf("%s",str);
		if (strcmp(str,".end.") != 0)
		{
			*(tquery+x) = str;
		        x++;
		}
	}
	while((strcmp(str,".end.") != 0) && (x <= 3));
	search(tquery,&records_t[0],lk,items_t,lenk,x);
}
